import { call, put, takeLatest } from 'redux-saga/effects';
import { BEFORE_BOOKS,booksList,SEARCH,updatePv,INCREASE } from '../actions/beforepage.action';
import Message from '../components/SimpleLayer/Message';
import API from '../services';
 
function showErr(content){
	Message.error({
		top:'6px',
		width:'300',
		content,
		delayAutoClose:2000,
		yes:false
	});
}

export function* IncreasePvSaga() {
  yield takeLatest(INCREASE, function* (action) {
    try {
      const res = yield call(API.IncreasePv,action.data);
      if (res.success) {
        yield put(updatePv({id:action.data.id,pv:res.result}));
      }else{
        showErr(res.result);
      }
    } catch (e) {
      showErr(e.message);
    }
  });
}

export function* BooksSaga() {
  yield takeLatest(BEFORE_BOOKS, function* (action) {
   	try {
      const res = yield call(API.BeforePageBooks);
  		if (res.success) {
  			yield put(booksList(res.result));
			}else{
				showErr(res.result);
			}
   	} catch (e) {
      showErr(e.message);
   	}
	});
}

export function* SearchSaga() {
  yield takeLatest(SEARCH, function* (action) {
   	try {
      const res = yield call(API.Search,action.data);
  		if (res.success) {
  			yield put(booksList(res.result));
			}else{
				showErr(res.result);
			}
   	} catch (e) {
      showErr(e.message);
   	}
	});
}